<template>
  <div
    :class="[$route.path === '/home' ? 'article-home' : '']"
    ref="article-list"
  >
    <van-skeleton title avatar :row="8" :loading="skeletonLoading">
      <div v-if="data">
        <div class="item" v-for="item in  data" :key="item.id">
        <div class="avatar" v-if="showAvatar" @click="goToUser">
          <van-image
            width="0.9rem"
            height="0.9rem"
            fit="cover"
            round
            :src="item.user.avatar"
          />
          <div class="author">
            <a>{{ item.user.nickname }}</a>
            <span>已关注 · {{item.user.introduce}}</span>
          </div>
        </div>
        <div
          class="top"
          v-if="cover.type === 0 || cover.type === 2 || cover.type === 3"
          @click="goToArticle(item.id)"
        >
          <h1>{{ item.title }}</h1>
        </div>
        <div class="centre" v-if="cover.type === 2" @click="goToArticle(item.id)">
          <van-image fit="cover" :src="item.cover_img" />
        </div>
        <div class="centre-3" v-if="cover.type === 3" @click="goToArticle(item.id)">
          <van-image fit="cover" :src="item.cover_img" />
          <van-image fit="cover" :src="item.cover_img" />
          <van-image fit="cover" :src="item.cover_img" />
        </div>
        <div>
          <div class="bottom-1" v-if="cover.type === 1">
            <div class="left">
              <h1 @click="goToArticle(item.id)">{{ item.title }}</h1>
              <div class="left-bottom">
                <div>
                  <span class="author-name" v-if="!showAvatar">{{
                    item.user.nickname
                  }}</span>
                  <span class="comment">{{ item.comment_count }}评论</span>
                  <span class="read">{{ item.read_count }}观看</span>
                  <span class="time">{{ item.createdAt | relativeTime }}</span>
                </div>
                <div>
                  <i class="iconfont icon-cha" @click="onClickCha"></i>
                  <Popover />
                </div>
              </div>
            </div>
            <div class="right" @click="goToArticle(item.id)">
              <van-image
                fit="cover"
                :src="item.cover_img"
              />
            </div>
          </div>
          <div
            class="bottom"
            v-if="cover.type === 0 || cover.type === 2 || cover.type === 3"
          >
            <div>
              <span class="author-name" v-if="!showAvatar">{{
                item.user.nickname
              }}</span>
              <span class="comment">{{ item.comment_count }}评论</span>
              <span class="read">{{ item.read_count }}观看</span>
              <span class="time">{{ item.createdAt | relativeTime }}</span>
            </div>
            <div>
              <i class="iconfont icon-cha" @click="onClickCha"
                ><Popover :placement="'left-end'"
              /></i>
            </div>
          </div>
        </div>
        </div>
      </div>
    </van-skeleton>

  </div>
</template>

<script>
import bus from '@/utils/bus'
import Popover from '@/components/My-Popover.vue'
import { debounce } from 'lodash'
export default {
  name: 'ArticleList',
  components: { Popover },
  props: {
    data: {
      type: Array,
      required: true // 暂时不必填
    }
  },
  data () {
    return {
      showAvatar: true,
      cover: {
        type: 1, // 封面类型
        images: [] // 图片数组
      },
      isCha: false,
      loading: false,
      skeletonLoading: true,
      finished: false,
      timestamp: null,
      scrollTop: 0, // 列表滚动到顶部的距离

    }
  },
  created(){
    this.isData()
  },
  mounted () {
    const articleItem = this.$refs['article-list']
    articleItem.onscroll = debounce(() => {
      this.scrollTop = articleItem.scrollTop
    }, 50)
  },
  activated () {
     this.isData()

    // 把记录的到顶部距离重新设置回去
    this.$refs['article-list'].scrollTop = this.scrollTop
  },
  methods: {
    isData(){
      if(this.data) return this.skeletonLoading = false
    },
    goToArticle (id) {
      this.$router.push(`/article/${id}`)
    },
    goToUser () {
      this.$router.push('/user')
    },
    onClickCha () {
      console.log('点击了差')
      this.isCha = true
      bus.$emit('onClickCha')
    }
  }
}
</script>
<style lang="scss" scoped>
.article-home {
  position: fixed;
  left: 0;
  right: 0;
  top: 97.95px;
  bottom: 50px;
  overflow-y: auto;
  background-color: rgb(245, 243, 243);
}

.item {
  display: flex;
  flex-direction: column;
  padding: 15px 15px 10px 15px;
  box-sizing: border-box;
  // height:330px;
  margin-top: 10px;
  background-color: #fff;
  .avatar {
    display: flex;
    margin-bottom: 10px;
    .author {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      margin-left: 10px;
      a {
        font-size: 14px;
        font-weight: 500;
      }
      span {
        font-size: 10px;
        color: #ccc;
      }
    }
  }
  .top {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    text-align: left;
    h1 {
      font-size: 16px;
      line-height:23px;
      margin: 0;
      overflow: hidden; //溢出内容隐藏
        text-overflow: ellipsis; //文本溢出部分用省略号表示
        display: -webkit-box; //特别显示模式
        -webkit-line-clamp: 2; //行数
        -webkit-box-orient: vertical; //盒子中内容竖直排列
    }
  }
  .centre {
    // border-radius:10px;
    width: 100%;
    height: 200px;
    border-radius: 4px;
        overflow: hidden;
    // display: flex;
    // justify-content: space-between;
    margin-top: 5px;
    .van-image {
      width: 100%;
      height: 200px;
    }
  }
  .centre-3 {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    border-radius: 4px;
        overflow: hidden;
    .van-image {
      width: 114px;
      height: 90px;
    }
  }

  .bottom {
    // margin:0;
    // padding:0;
    display: flex;
    justify-content: space-between;
    .icon-cha {
      font-size: 12px;
      color: #ccc;
    }
    span {
      font-size: 12px;
      color: #ccc;
      margin-right: 10px;
    }
  }
  .bottom-1 {
    display: flex;
    box-sizing: border-box;
    height: 90px;
    .left {
      // position: relative;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      text-align: left;
      width: 230px;
      margin-right: 14px;
      h1 {
        font-size: 16px;
        line-height:23px;
        margin: 0;
        overflow: hidden; //溢出内容隐藏
        text-overflow: ellipsis; //文本溢出部分用省略号表示
        display: -webkit-box; //特别显示模式
        -webkit-line-clamp: 2; //行数
        -webkit-box-orient: vertical; //盒子中内容竖直排列
      }
      .left-bottom {
        display: flex;
        justify-content: space-between;

        .icon-cha {
          font-size: 12px;
          color: #ccc;
        }
        span {
          font-size: 12px;
          color: #ccc;
          margin-right: 10px;
        }
      }
    }
    .right {
      // width: 120px;
      // height: 99px;
      // border-radius: 4px;
      // overflow: hidden;
      .van-image{
        width: 120px;
        height: 85px;
        border-radius: 4px;
        overflow: hidden;
      }
    }
  }
}
</style>
